package emulator.shell.GUI.symbols;

import java.awt.BorderLayout;

import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.GroupLayout.Alignment;

import emulator.assembler.SymbolTableEntry;
import emulator.hardware.HwWord;
import emulator.shell.GUI.util.DialogBase;

@SuppressWarnings("serial")
public class EditSymbolDialog extends DialogBase
{
	private String remark;
	private String label;
	private int type;
	private JTextField fieldSymbol;
	private JTextField fieldRemark;
	private JComboBox fieldType;

	public EditSymbolDialog(JFrame owner, SymbolTableEntry symbol)
	{
		super(owner, "Edit symbol", true);
		buildContents(symbol);
		pack();
		setLocationRelativeTo(null);
	}

	private void buildContents(SymbolTableEntry symbol)
	{
		JLabel labelAddress = new JLabel("Address");
		JLabel labelSymbol = new JLabel("Label");
		JLabel labelRemark = new JLabel("Remark");
		JLabel labelType = new JLabel("Type");
		
		JLabel fieldAddress = new JLabel("$"+new HwWord(symbol.getAddress()));
		fieldSymbol = new JTextField(symbol.getLabel());
		fieldRemark = new JTextField(symbol.getRemark());
		fieldType = new JComboBox(new String[] { "", "Code", "Data", "Word", "Text", "Chip", "Empty" });
		fieldType.setSelectedIndex(symbol.getType());

		JPanel editPanel = new JPanel();
		GroupLayout layout = new GroupLayout(editPanel);
		editPanel.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(labelAddress)
						.addComponent(labelSymbol)
						.addComponent(labelRemark)
						.addComponent(labelType))
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(fieldAddress)
						.addComponent(fieldSymbol)
						.addComponent(fieldRemark)
						.addComponent(fieldType))
				);
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
						.addComponent(labelAddress)
						.addComponent(fieldAddress)
						)
				.addGroup(layout.createParallelGroup()
						.addComponent(labelSymbol)
						.addComponent(fieldSymbol)
						)
				.addGroup(layout.createParallelGroup()
						.addComponent(labelRemark)
						.addComponent(fieldRemark)
						)
				.addGroup(layout.createParallelGroup()
						.addComponent(labelType)
						.addComponent(fieldType)
						)
				);
	    layout.linkSize(SwingConstants.HORIZONTAL, labelAddress, labelSymbol, labelRemark, labelType);
		
		getContentPane().setLayout(new BorderLayout());
		add(editPanel, BorderLayout.CENTER);
		add(createButtonPanel(), BorderLayout.PAGE_END);
	}

	@Override
	protected void closeDialog(boolean result)
	{
		if (result)
		{
			label = fieldSymbol.getText();
			remark = fieldRemark.getText();
			type = fieldType.getSelectedIndex();
		}
		super.closeDialog(result);
	}

	public String getRemark()
	{
		return remark;
	}

	public String getLabel()
	{
		return label;
	}

	public int getSymbolType()
	{
		return type;
	}
}
